#include "Enemy.hh"
#include <iostream>

using namespace std;

// 构造函数,初始化敌人属性
Enemy::Enemy(int id, const std::string& name, int hp, int atk, int def, int speed)
    : id_(id), name_(name), hp_(hp), atk_(atk), def_(def), speed_(speed) {}

// ===== ICharacter接口实现 =====

// 检查敌人是否存活
bool Enemy::isAlive() const {
    return hp_ > 0;
}

// 获取敌人ID
int Enemy::getID() const { return id_; }

// 敌人受到伤害
bool Enemy::takeDamage(unsigned damage) {
    if (damage >= hp_) hp_ = 0;
    else hp_ -= damage;
    return isAlive();
}

// 敌人攻击目标
bool Enemy::attack(ICharacter& target) {
    unsigned damage = atk_ > target.getDefense() ? atk_ - target.getDefense() : 0;
    cout << name_ << " attacked " << target.getName() << " dealing " << damage << " damage." << endl;
    return target.takeDamage(damage);
}

// 获取基本属性
std::string Enemy::getName() const { return name_; }
std::string Enemy::getDescription() const { return "dangerous enemy"; }
unsigned Enemy::getHealth() const { return hp_; }
unsigned Enemy::getAttack() const { return atk_; }
unsigned Enemy::getDefense() const { return def_; }

// 获取状态描述字符串
std::string Enemy::toString() const {
    return "Enemy: " + name_ +
           " HP: " + to_string(hp_) +
           " ATK: " + to_string(atk_) +
           " DEF: " + to_string(def_) +
           " SPD: " + to_string(speed_);
}

// 敌人不使用物品栏
void Enemy::addToInventory(Item* item) {
    // Enemy has no inventory, no need to implement
}

void Enemy::displayInventory() const {
    // Enemy has no inventory, no need to implement
}

// 显示敌人状态
void Enemy::display() const {
    cout << toString() << endl;
}

// 检查敌人是否已被击败
bool Enemy::isDefeated() const {
    return isDefeated_;
}

// 设置敌人击败状态
void Enemy::setDefeated(bool defeated) {
    isDefeated_ = defeated;
}

// 获取和设置速度值
int Enemy::getSpeed() const { return speed_; }
void Enemy::setSpeed(int speed) {
    if (speed < 0) speed_ = 0;
    else if (speed > 100) speed_ = 100;
    else speed_ = speed;
}

// JSON解析函数
void from_json(const json& j, Enemy& e) {
    j.at("id").get_to(e.id_);
    j.at("name").get_to(e.name_);
    j.at("hp").get_to(e.hp_);
    j.at("atk").get_to(e.atk_);
    j.at("def").get_to(e.def_);
    j.at("speed").get_to(e.speed_);
}